<template>
  <el-dialog ref="dislogRef" @close="dialogVisibleClose" :visible.sync="dialogVisible" lock-scroll width="60%"
    min-height="60%">
    <div slot="title" class="header_title">
      <span class="title_span">{{ logTitle }}</span>
    </div>

    <div class="content_div">
      <div class="info_table">
        <div class="div_table">
          <div style="margin-bottom: 20px;">
            <!--  -->
            <el-button type="primary" @click="handleDetail">查看详情</el-button>
          </div>
          <div class="table_content">
            <!--  -->
            <el-table  ref="multipleTable" :data="tableDatas" :border="false" @selection-change="onClickRow" row-key="index" :highlight-current-row="true"
              height="80%" v-loading="tableDataLoading" element-loading-text="拼命加载中"
              element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0)"
              :show-summary="true"
              :summary-method="getSummaries"
              >
              <!-- v-if="!isRadio" -->
              <el-table-column type="selection" width="60" :reserve-selection="false"></el-table-column>
              <el-table-column type="index" width="50" label="序号"></el-table-column>
              <el-table-column v-for="item in tableData" :key="item.morenName" :label="item.morenName"
                :width="item.cellWidth || ''" min-width="100px" :prop="item.props" show-overflow-tooltip></el-table-column>
            </el-table>
          </div>
        </div>
      </div>
      <div class="info_button">
        <el-button type="primary" @click.stop="dialogVisible = false">关闭</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import { getAction } from '@/api/manage'
import { handlePermissions, handleOtherPerms, getSystemParamsByName, formatMoney, formatPrice, formatNumber, delcommafy, handleArrayToObject, handleObjectToArray } from '@/utils/index'
export default {
  name: 'dialogOperationLog',
  props: {
    logTitle: String,
  },
  mounted() {
    this.multipleSelection = null
  },
  data() {
    return {
      pagesize: 20, //每页的数据条数
      currentPage: 1, //默认开始页面
      tableDataLoading: false,
      tableDatas: [],
      tableData: [
        {
          morenName: '单据日期',
          props: 'createTime',
          zidingyiState: true,
          // cellWidth: '120px',
          align: 'center'
        },
        {
          morenName: '单据编号',
          props: 'orderNumber',
          zidingyiState: true,
          // cellWidth: '120px',
          align: 'center'
        },
        {
          morenName: '单据类型',
          props: 'orderType_dictText',
          zidingyiState: true,
          cellWidth: '100px',
          align: 'center'
        },
        {
          morenName: '客户名称',
          props: 'xghCompanyId_dictText',
          zidingyiState: true,
          // cellWidth: '120px',
          align: 'center'
        },
        {
          morenName: '经手人',
          props: 'sysUserId_dictText',
          zidingyiState: true,
          cellWidth: '100px',
          zidingyiName: '',
            morenState: true,
            zidingyiState: true,
           align: 'center'
        },
        {
          morenName: '执行数量',
          props: 'number',
          zidingyiState: true,
          cellWidth: '80px'
        },
        {
          morenName: '执行金额',
          props: 'collectionPrice',
          zidingyiState: true,
          cellWidth: '80px'
        },
        {
          morenName: '未执行数量',
          props: 'noNumber',
          zidingyiState: true,
          cellWidth: '100px'
        },
        {
          morenName: '未执行金额',
          props: 'noCollectionPrice',
          zidingyiState: true,
          cellWidth: '100px'
        },
        // {
        //   morenName: '执行状态',
        //   props: 'executionStatus',
        // }
      ],
      multipleSelection: null, // 存储多选的数据
      dialogVisible: false, // 新增弹框
      paramsData: {},
    }
  },
  watch: {
    dialogVisible(val) {
      if (val) {
        // this.getBillData()
      } else {
        this.paramsData = {}
      }
    }
  },
  methods: {
    async getData(obj) {
      // console.log(obj,'obj')
      // this.paramsData = obj
      // console.log(this.paramsData,'this.paramsData')
      // console.log(this.dialogVisible,'this.dialogVisible')
      // 避免提前请求
      // if (!this.dialogVisible) {
      //   return
      // }
      // 获取单据数据
      const res = await getAction('/xghSaleStorehouseOrder/xghSaleStorehouseOrder/StorehouseExecutionDetailsList', obj)
      if (res.code === 200) {
        // console.log(res,'res1');
        this.tableDatas = res.result
      }
    },
    async getBillData() {
      if (this.paramsData.xghSaleOrderId === -1) {
        return
      }
      let url = '/xghSaleStorehouseOrder/xghSaleStorehouseOrder/StorehouseExecutionDetailsList'
      console.log(this.logTitle, 'this.logTitle');

      if (this.logTitle == '查看订单执行明细') {
        this.paramsData.billId = this.paramsData.xghSaleOrderId
        url = '/xghSaleStorehouseOrder/xghSaleStorehouseOrder/StorehouseExecutionDetailsList'
      }
      // 获取单位列表数据
      const res = await getAction(url, this.paramsData)
      console.log(res, 'res2');
      if (res.code === 200) {

        // this.tableData = res.result
        // if (this.logTitle == '单据操作日志') {
        //   this.tableData.forEach((item, index) => {
        //     item.createTime = item.operationTime
        //   })
        // }
      }
    },
    //查看详情 跳转到销售出库
    handleDetail() {
      if (this.multipleSelection == null) {
        this.$message.warning('请选择要查看的单据')
        return
      } else {
        console.log(this.multipleSelection, 'this.multipleSelection');
        // 跳转到销售出库
        this.$toBillDetail(this, '6', this.multipleSelection)
        this.tableDatas = []
        this.dialogVisible = false
        this.multipleSelection = null
      }
    },
    // 选中当前行
    onClickRow(row) {
      console.log(row, 'row1');
      if (row.length > 0) {
        this.multipleSelection = row[0].id;
      } else {
        this.multipleSelection = null
      }
      console.log(this.multipleSelection, 'this.multipleSelection');
    },
    // 合计
    getSummaries(param) {
      // console.log(param, 'param');
      // return ['合计']
      this.$nextTick(() => {
        this.$refs.multipleTable.doLayout()
      })
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (column.property == 'number' ||  column.property == 'noNumber') {
          const values = data.map((item) => delcommafy(item[column.property]))
          if (!values.every((value) => isNaN(value))) {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr)
              if (!isNaN(value)) {
                return prev + curr
              } else {
                return prev
              }
            }, 0)
            sums[index] = formatMoney(sums[index])
          }
        } else {
          sums[index] = ''
        }
      })
      return sums
    },
    dialogVisibleClose() {
      this.tableDatas = []
      this.multipleSelection = null
      this.dialogVisible = false
      // 清空表格选中状态
  // if (this.$refs.multipleTable) {
  //   this.$refs.multipleTable.clearSelection()
  // }
    },
  },
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog {
  width: 100%;
  margin-top: 20vh !important;

  .el-dialog__body {
    padding: 0;
  }

  .el-dialog__header {
    width: 100%;
    height: 50px;
    box-sizing: border-box;
    padding: 0 0.187512rem 0 0.281268rem;
    border-bottom: 1px solid #eeeeee;
    display: flex;
    align-items: center;
    justify-content: space-between;

    .el-dialog__headerbtn {
      position: static;

      .el-icon-close {
        color: #999;
        font-size: 25px;
      }
    }
  }

  .header_title {
    width: 100%;
    display: flex;
    align-items: center;

    .title_span {
      white-space: nowrap;
      font-size: 16px;
      color: #666;
      font-weight: 600;
    }
  }
}

.content_div {
  width: 100%;
  height: 50vh;
  background: #fff;
  box-sizing: border-box;
  padding: 0.187512rem 0.281268rem;

  .info_table {
    width: 100%;
    height: calc(100% - 60px);
    display: flex;

    .div_table {
      width: 100%;
      height: 100%;
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .table_content {
        height: 100%;

        ::v-deep .el-table {
          border: 1px solid #e3e3e3;
          box-sizing: border-box;

          th,
          td {
            padding: 0;
            color: #666;
            box-sizing: border-box;
            border-right: 1px solid #e3e3e3;

            .cell {
              height: 30px;
              display: flex;
              align-items: center;
              font-size: 12px;
            }

            &:first-child {
              .cell {
                justify-content: center;
              }
            }
          }

          th {
            .cell {
              background: #ededed;
              justify-content: center;
            }
          }

          .el-table__body tr:hover>td {
            background: #e3f6ff;
          }

          .el-table__footer-wrapper {
            td {
              background: #f4f4f4;
              border: none;
            }
          }

          //滚动条的宽度
          ::-webkit-scrollbar {
            width: 10px;
            height: 10px;
            background-color: #e5e5e5;
          }

          //滚动条的滑块
          ::-webkit-scrollbar-thumb {
            background-color: #c9c9c9;
          }
        }
      }

      .total_record {
        width: 100%;
        height: 0.562535rem;
        box-sizing: border-box;
        padding: 0 0.281268rem;
        border: 1px solid #e3e3e3;
        border-top: none;
        background: #f4f4f4;
        display: flex;

        p {
          margin: 0;
          line-height: 0.562535rem;
          padding-right: 0.281268rem;
          font-size: 0.225rem;
          font-family: PingFang SC;
          font-weight: 500;
          color: #666666;
        }
      }
    }
  }

  .info_button {
    width: 100%;
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: flex-end;

    ::v-deep .el-button {
      width: 90px;
      height: 40px;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 5px;
    }
  }
}
</style>